package com.keur.money.app

import android.annotation.SuppressLint
import android.content.Context
import androidx.core.content.ContextCompat
import com.keur.money.BuildConfig
import com.keur.money.R
import com.keur.money.base.BaseApplication
import com.keur.money.base.utils.log.MullaLog
import com.keur.money.http.HttpHelper
import com.keur.money.http.OkHttpLoader
import com.keur.money.utils.event.KeurBehaviorTracker
import com.keur.money.utils.event.KeurPublisher
import com.keur.money.utils.helper.KeurAnalysisHelper
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.MaterialHeader
import com.scwang.smart.refresh.layout.SmartRefreshLayout

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/24 14:20
 * @Annotation：
 */
class MyApp : BaseApplication(){

    //事件订阅
    lateinit var publisher: KeurPublisher

    companion object {
        // 全局Context
        @SuppressLint("StaticFieldLeak")
        lateinit var context: Context

        @SuppressLint("StaticFieldLeak")
        lateinit var app: MyApp

        //static 代码段可以防止内存泄露
        init {
            //设置全局的Header构建器
            SmartRefreshLayout.setDefaultRefreshHeaderCreator { context, layout ->
                layout.setPrimaryColorsId(R.color.transparent, R.color.theme) //全局设置主题颜色
                MaterialHeader(context).setColorSchemeColors(ContextCompat.getColor(context,R.color.theme)) //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
            //设置全局的Footer构建器
            SmartRefreshLayout.setDefaultRefreshFooterCreator { context, _ -> //指定为经典Footer，默认是 BallPulseFooter
                ClassicsFooter(context).setDrawableSize(20f)
            }
        }
    }

    override fun attachBaseContext(base: Context) {
        super.attachBaseContext(base)
        context = base
        app = this
        publisher = KeurPublisher()
    }

    override fun onCreate() {
        super.onCreate()
        initLibConfigs()
    }

    fun getAppContext(): Context{
        return context
    }

    override fun onTerminate() {
        KeurBehaviorTracker.getInstance(this).release()
        super.onTerminate()
    }

    /**
     * 初始化Lib
     */
    private fun initLibConfigs(){
        MullaLog.debug(BuildConfig.isDebug)
        HttpHelper.init(OkHttpLoader())
        //初始化 analysis
        KeurAnalysisHelper.initAnalysisLibAtAppLaunch()
        //初始化用户轨迹
        KeurBehaviorTracker.getInstance(this)
        //设置自动上报时间 60分钟自动上报一次
        KeurBehaviorTracker.getInstance(this).startScheduledReporting(60 * 60 * 1000)
    }

    /**
     * 获取当前手机屏幕的宽
     */
    fun getPhoneWidth(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.widthPixels
    }

    /**
     * 获取当前手机屏幕的高
     */
    fun getPhoneHeight(): Int {
        // 手机屏幕的宽
        // 获取DisplayMetrics对象
        val displayMetrics = resources.displayMetrics
        return displayMetrics.heightPixels
    }
}